package com.hycreate.platform.modules.project.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.modules.project.product.SfProjectProduct;
import com.hycreate.platform.modules.project.product.SfProjectProductService;

/**
 * 项目产品关系Controller
 * @author hoader
 * @version 2017-01-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/product/sfProjectProduct")
public class SfProjectProductController extends BaseController {

	@Autowired
	private SfProjectProductService sfProjectProductService;
	
	@ModelAttribute
	public SfProjectProduct get(@RequestParam(required=false) String id) {
		SfProjectProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sfProjectProductService.get(id);
		}
		if (entity == null){
			entity = new SfProjectProduct();
		}
		return entity;
	}
	
	@RequiresPermissions("project:product:sfProjectProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(SfProjectProduct sfProjectProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SfProjectProduct> page = sfProjectProductService.findPage(new Page<SfProjectProduct>(request, response), sfProjectProduct); 
		model.addAttribute("page", page);
		return "modules/project/product/sfProjectProductList";
	}

	@RequiresPermissions("project:product:sfProjectProduct:view")
	@RequestMapping(value = "form")
	public String form(SfProjectProduct sfProjectProduct, Model model) {
		model.addAttribute("sfProjectProduct", sfProjectProduct);
		return "modules/project/product/sfProjectProductForm";
	}

	@RequiresPermissions("project:product:sfProjectProduct:edit")
	@RequestMapping(value = "save")
	public String save(SfProjectProduct sfProjectProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sfProjectProduct)){
			return form(sfProjectProduct, model);
		}
		sfProjectProductService.save(sfProjectProduct);
		addMessage(redirectAttributes, "保存项目产品关系成功");
		return "redirect:"+Global.getAdminPath()+"/project/product/sfProjectProduct/?repage";
	}
	
	@RequiresPermissions("project:product:sfProjectProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(SfProjectProduct sfProjectProduct, RedirectAttributes redirectAttributes) {
		sfProjectProductService.delete(sfProjectProduct);
		addMessage(redirectAttributes, "删除项目产品关系成功");
		return "redirect:"+Global.getAdminPath()+"/project/product/sfProjectProduct/?repage";
	}

}